/*=============================================================================
// File Name:   drv_gpio.c
// Author:      Wood Wang
// Date:        2022/10/1
// Version:     v1.0

=============================================================================*/
/* Includes -----------------------------------------------------------------*/
#include "fsl_usart.h"
#include "drv_gpio.h"

void  drv_gpio_init(void)
{
    /* Enables clock for IOCON.: enable */
    CLOCK_EnableClock(kCLOCK_Iocon);
    /* Enables the clock for the GPIO0 module */
    CLOCK_EnableClock(kCLOCK_Gpio0);
    CLOCK_EnableClock(kCLOCK_Gpio1);
    
    /* Init GPIO. */
    GPIO_PortInit(GPIO, 0);
    GPIO_PortInit(GPIO, 1);
    
    gpio_pin_config_t _gpio_l_config = {
        .pinDirection = kGPIO_DigitalOutput,
        .outputLogic = 0U,
    };
    gpio_pin_config_t _gpio_h_config = {
        .pinDirection = kGPIO_DigitalOutput,
        .outputLogic = 1U,
    };
    GPIO_PinInit(GPIO, 0, 14, &_gpio_h_config);    //L1    led
    GPIO_PinInit(GPIO, 0, 29, &_gpio_h_config);    //L2
    GPIO_PinInit(GPIO, 0, 23, &_gpio_h_config);    //L3
    GPIO_PinInit(GPIO, 0, 30, &_gpio_h_config);    //L4
    GPIO_PinInit(GPIO, 0, 22, &_gpio_h_config);    //L5
    GPIO_PinInit(GPIO, 0, 21, &_gpio_h_config);    //L6
    GPIO_PinInit(GPIO, 0, 20, &_gpio_h_config);    //L7
    GPIO_PinInit(GPIO, 0, 19, &_gpio_h_config);    //L8
    GPIO_PinInit(GPIO, 0, 18, &_gpio_h_config);    //L9

    
    gpio_pin_config_t KEY_config = {
        .pinDirection = kGPIO_DigitalInput,
        .outputLogic = 1U,
    };
    GPIO_PinInit(GPIO, 0, 12, &KEY_config);     //ISP KEY
//    GPIO_PinInit(GPIO, 0,  5, &KEY_config);     //RST KEY
    GPIO_PinInit(GPIO, 0,  4, &KEY_config);     //WAKEUP KEY
    
    const uint32_t LED_DATA = (/* Selects pull-up function */
                              IOCON_PIO_MODE_PULLUP |
                              /* Enable hysteresis */
                              IOCON_PIO_HYS_EN |
                              /* Input not invert */
                              IOCON_PIO_INV_DI |
                              /* Disables Open-drain function */
                              IOCON_PIO_OD_DI |
                              /* Bypass input filter */
                              IOCON_PIO_SMODE_BYPASS |
                              /* IOCONCLKDIV0 */
                              IOCON_PIO_CLKDIV0);
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_14, LED_DATA);   //L1
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_29, LED_DATA);   //L2
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_23, LED_DATA);   //L3
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_30, LED_DATA);   //L4
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_22, LED_DATA);   //L5
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_21, LED_DATA);   //L6
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_20, LED_DATA);   //L7
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_19, LED_DATA);   //L8
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_18, LED_DATA);   //L9
    
    const uint32_t KEY_DATA = (/* Selects pull-up function */
                              0 |
                              /* Enable hysteresis */
                              IOCON_PIO_HYS_EN |
                              /* Input not invert */
                              IOCON_PIO_INV_DI |
                              /* Disables Open-drain function */
                              IOCON_PIO_OD_DI |
                              /* Bypass input filter */
                              IOCON_PIO_SMODE_BYPASS |
                              /* IOCONCLKDIV0 */
                              IOCON_PIO_CLKDIV0);
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_12, KEY_DATA);   //
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_4 , KEY_DATA);   //


    const uint32_t DEBUG_UART_RX = (/* Selects pull-up function */
                                    IOCON_PIO_MODE_PULLUP |
                                    /* Enable hysteresis */
                                    IOCON_PIO_HYS_EN |
                                    /* Input not invert */
                                    IOCON_PIO_INV_DI |
                                    /* Disables Open-drain function */
                                    IOCON_PIO_OD_DI |
                                    /* Bypass input filter */
                                    IOCON_PIO_SMODE_BYPASS |
                                    /* IOCONCLKDIV0 */
                                    IOCON_PIO_CLKDIV0);
    /* PIO1 PIN16 (coords: 36) is configured as USART0, RXD. */
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_24, DEBUG_UART_RX);
    
    const uint32_t DEBUG_UART_TX = (/* Selects pull-up function */
                                    IOCON_PIO_MODE_PULLUP |
                                    /* Enable hysteresis */
                                    IOCON_PIO_HYS_EN |
                                    /* Input not invert */
                                    IOCON_PIO_INV_DI |
                                    /* Disables Open-drain function */
                                    IOCON_PIO_OD_DI |
                                    /* Bypass input filter */
                                    IOCON_PIO_SMODE_BYPASS |
                                    /* IOCONCLKDIV0 */
                                    IOCON_PIO_CLKDIV0);
    /* PIO1 PIN17 (coords: 37) is configured as USART0, TXD. */
    IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_25, DEBUG_UART_TX);
    
    
    /* Enables clock for switch matrix.: enable */
    CLOCK_EnableClock(kCLOCK_Swm);
    /* USART0_TXD connect to P0_25 */
    SWM_SetMovablePinSelect(SWM0, kSWM_USART0_TXD, kSWM_PortPin_P0_25);
    /* USART0_RXD connect to P0_24 */
    SWM_SetMovablePinSelect(SWM0, kSWM_USART0_RXD, kSWM_PortPin_P0_24);
    /* Disable clock for switch matrix. */
    CLOCK_DisableClock(kCLOCK_Swm);
}


